﻿/*  ----------------------------------------------------------------------------
 *  Author:
 *  Chandra Shekhar Tripathi
 *  ----------------------------------------------------------------------------
 *  Product Name:
 *  DbDocumenter
 *  ----------------------------------------------------------------------------
 *  Year:
 *  2009
 *  ----------------------------------------------------------------------------
 *  File:
 *  HTMLManager.cs
 *  ----------------------------------------------------------------------------
 */
namespace DBDocumenter.BLL.Services.HTML
{
    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.XPath;
    using System.Xml.Xsl;

    using DBDocumenter.Exception;

    /// <summary>
    /// 
    /// </summary>
    class HTMLManager
    {
        #region Fields

        private static HTMLManager instance = new HTMLManager();

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="HTMLWriter"/> class.
        /// </summary>
        public HTMLManager()
        {
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static HTMLManager Instance
        {
            get { return HTMLManager.instance; }
            set { HTMLManager.instance = value; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Copies the directory.
        /// </summary>
        /// <param name="SourcePath">The source path.</param>
        /// <param name="DestinationPath">The destination path.</param>
        /// <param name="overwriteexisting">if set to <c>true</c> [overwriteexisting].</param>
        /// <returns></returns>
        public static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
        {
            bool ret = false;
            try
            {
                SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
                DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

                if (Directory.Exists(SourcePath))
                {
                    if (Directory.Exists(DestinationPath) == false)
                        Directory.CreateDirectory(DestinationPath);

                    foreach (string fls in Directory.GetFiles(SourcePath))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
                    }
                    foreach (string drs in Directory.GetDirectories(SourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
                            ret = false;
                    }
                }
                ret = true;
            }
            catch (Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Error, ex.ToString());
                ret = false;
            }
            return ret;
        }

        /// <summary>
        /// Writes the HTML.
        /// </summary>
        /// <param name="xmlFileName">Name of the XML file.</param>
        /// <param name="xslFileName">Name of the XSL file.</param>
        /// <param name="htmlFileName">Name of the HTML file.</param>
        public static void WriteHTML(string xmlFileName, string xslFileName, string htmlFileName)
        {
            try
            {
                XmlTextReader xmlSource = new XmlTextReader(@"xml\" + xmlFileName);
                XPathDocument xpathDoc = new XPathDocument(xmlSource);

                XmlTextReader xslSource = new XmlTextReader(@"xsl\" + xslFileName);
                XslCompiledTransform xsltDoc = new XslCompiledTransform();

                xsltDoc.Load(xslSource);

                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);

                XsltArgumentList ar = new XsltArgumentList();

                xsltDoc.Transform(xpathDoc, null, sw);

                File.WriteAllText(@"html\" + htmlFileName, sb.ToString());
                sw.Close();
            }
            catch (Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Error, ex.ToString());
            }
        }

        #endregion Methods
    }
}